package com.gzdh.tang.modu.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gzdh.tang.config.ali.AliPayUtil;
import com.gzdh.tang.modu.enumc.IotPayAction;
import com.gzdh.tang.modu.enumc.ProductCode;
import com.gzdh.tang.modu.enumc.Scene;
import com.gzdh.tang.modu.model.WithholdReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Random;

@Slf4j
public class CloudSaleApiUtil {


    public static WithholdReq.BizContent getFacePayBizContent(String outTradeNo, String amount, String uid,
                                                              String deviceNo, String authCode,
                                                              String terminalParams, String isvPid,
                                                              String subject, String enterprisePayAmount,
                                                              boolean isFace) {
        return getBizContent(outTradeNo,amount, uid, deviceNo, authCode,
                terminalParams, isvPid, subject, null, null, enterprisePayAmount, isFace, true);
    }


    public static JSONArray getGoodDetail(String amount, JSONArray goods) {
        if (goods != null && !goods.isEmpty()) {
            return goods;
        }
        JSONArray array = new JSONArray();
        JSONObject goodJson = new JSONObject();
        goodJson.put("goods_id", "default");
        goodJson.put("goods_name", "货柜消费");
        goodJson.put("quantity", 1);
        goodJson.put("price", amount);
        array.add(goodJson);
        return array;
    }

    private static String getOutTradeNoRandom() {
        StringBuilder random = new StringBuilder();
        random.append(DateUtil.getCurrentDateTime());
        for (int i = 0; i < 3; i++) {
            random.append(new Random().nextInt(10));
        }
        return random.toString();
    }

    private static WithholdReq.BizContent getBizContent(String outTradeNo, String amount, String uid,
                                                        String deviceNo, String authCode,
                                                        String terminalParams, String isvPid,
                                                        String subject, JSONArray goods,
                                                        String senceNo, String enterprisePayAmount,
                                                        boolean isFace, boolean isWithhold) {
        WithholdReq.BizContent bizContent = new WithholdReq.BizContent();
        bizContent.total_amount = amount;
        //商家交易号,需保证在商户应用下唯一
//        bizContent.out_trade_no = getOutTradeNoRandom();
        bizContent.out_trade_no = outTradeNo;
        bizContent.subject = subject;
        bizContent.user_id = uid;
        //设备编号
        bizContent.terminal_id = deviceNo;
        //支付授权码
        bizContent.auth_code = authCode;
        //设备返佣参数
        bizContent.terminal_params = JSONObject.parseObject(terminalParams);
        //商品
        bizContent.goods_detail = getGoodDetail(amount, goods).toString();

        //isv签约返佣协议账户的pid
        WithholdReq.ExtendParams extendParams = new WithholdReq.ExtendParams();
        extendParams.sys_service_provider_id = isvPid;
        bizContent.extend_params = extendParams;

        WithholdReq.BusinessParams businessParams = new WithholdReq.BusinessParams();
//        businessParams.iot_pay_action = isFace ? IotPayAction.online_face_pay.name() : IotPayAction.bar_pay.name();
        businessParams.assignJointAccountId = AliPayUtil.getAssignJointAccountId();
        businessParams.enterprisePayAmount = enterprisePayAmount;
        bizContent.business_params = businessParams;

        //产品码-代扣
        bizContent.product_code = isWithhold ? ProductCode.GENERAL_WITHHOLDING_P.name() : ProductCode.FACE_TO_FACE_PAYMENT.name();
        //支付场景 220811新增
        bizContent.scene = isFace ? Scene.security_code.name() : Scene.bar_code.name();
        if (isFace) {
            if (authCode.startsWith("AAAA"))
                bizContent.scene = Scene.codec_double_offline_code.name();
            else
                bizContent.scene = Scene.security_code.name();
        } else
            bizContent.scene = Scene.bar_code.name();

        if (!StringUtils.isEmpty(senceNo))
            bizContent.sence_no = senceNo;

        log.info("getBizContent" +
                "\nisFace: " + isFace +
                "\nisWithhold: " + isWithhold +
                "\nauthCode: " + authCode +
                "\nscene: " + bizContent.scene +
                "\nsence_no: " + bizContent.sence_no +
                "\nterminalParams: " + bizContent.terminal_params +
                "\nbusiness_params:" + bizContent.business_params);
        return bizContent;
    }
}
